package wnhz.com.business.groupshop.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.groupshop.adapter.MianTwoAdapter;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.groupshop.bean.OrderBean;
import wnhz.com.business.groupshop.ui.MyRemindActivity;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.decoration.HorizDecoration;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.view.TranslucentActionBar;


/**
 * 拼抢
 */
public class HomeFragment2 extends BaseFragment implements BaseRecyclerAdapter.OnDataLoadFinish, ActionBarClickListener {
    static final String TAG = "HomeFragment2";

    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;


    private View view;
    private BaseActivity activity;
    private MianTwoAdapter mianTwoAdapter;
    private List<OrderBean.InfoBean> orderInfos = new ArrayList();

    public static HomeFragment2 newInstance() {
        HomeFragment2 homeFragment2 = new HomeFragment2();
        return homeFragment2;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_fragment2, container, false);
        ButterKnife.bind(this, view);
        activity = (BaseActivity) getActivity();
        actionbar.setData("拼抢", 0, null, R.drawable.ic_pinqiang_tixing, "我的提醒", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        LoadData();
        initView();

        return view;
    }

    private void LoadData() {
        if (MyApplication.getInstance().getInfoBean() == null){
            MyApplication.getInstance().BackLoginActivity();
            return;
        }
        OkGo.<String>post(Url.GOODS_COMPETING)
                .tag(this)
                .params("token",MyApplication.getInstance().getInfoBean().getToken())
                .params("goods_type","1")//1.拼团  2.返利  3.免费
                .execute(new DialogCallback<String>(activity) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("拼抢onSuccess",response.body().toString());
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            String re = jsonObject.optString("re");
                            if (re.equals("1")){

                            }else{

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showToast(activity,"获取信息失败");
                    }
                });
    }

    private void initView() {
        mianTwoAdapter = new MianTwoAdapter(activity, orderInfos);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new HorizDecoration(20));
        mianTwoAdapter.setOnDataLoadFinish(this);
        mianTwoAdapter.loadDataFirstTime();
        recyclerView.setAdapter(mianTwoAdapter);

        mianTwoAdapter.setOnClickListener(new MianTwoAdapter.MyItemClickLinstener() {
            @Override
            public void OnDrawbackClickListener(MianTwoAdapter adapter, int position, View view) {
                ToastUtils.showToast(activity, "已提醒");
            }
        });
    }

    @Override
    public void loadDataFinished(int datasLength) {
        if (datasLength == 0) {
            emptyLayout.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {
        startActivity(MyRemindActivity.createIntent(getContext()));
    }

    @Override
    public void onEditTextClick() {

    }
}
